package cn.sdut.thrift.server;

import cn.sdut.thrift.HelloWorldService;
import cn.sdut.thrift.service.HelloWorldImpl;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.server.TNonblockingServer;
import org.apache.thrift.transport.TFastFramedTransport;
import org.apache.thrift.transport.TNonblockingServerSocket;
import org.apache.thrift.transport.TTransportException;

/**
 * @author MaJiatao
 * @description: 非阻塞式IO
 * @created: 2019/10/22
 */
public class HelloServerDemoWithNIO {
    public static final int SERVER_PORT = 8090;

    public void startServer() {
        try {
            HelloWorldService.Processor<HelloWorldService.Iface> ifaceProcessor = new HelloWorldService.Processor<>(new HelloWorldImpl());

            System.out.println("TNonblockingServer start...");

            TNonblockingServerSocket tNonblockingServerSocket = new TNonblockingServerSocket(SERVER_PORT);
            TNonblockingServer.Args args = new TNonblockingServer.Args(tNonblockingServerSocket);
            args.processor(ifaceProcessor);
            args.transportFactory(new TFastFramedTransport.Factory());
            args.protocolFactory(new TBinaryProtocol.Factory());

            TNonblockingServer tNonblockingServer = new TNonblockingServer(args);
            tNonblockingServer.serve();
        } catch (TTransportException e) {
            System.out.println("TNonblockingServer start error!!!");
            e.printStackTrace();
        }

    }
}
